{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Llama-3-70b-Instruct 代理\n",
    "\n",
    "llama-3-70b-Instruct 代理一位专业的经济学家，并输出为 Markdown 格式\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 要点：\n",
    "- 🌐 以 API 的方式接入 LlaMa-3-70b-Instruct\n",
    "- ⚡️ Groq 加成，闪电生成\n",
    "- ⚙️ Syetem Prompt，提升中文回复的稳定性 & 其他增益\n",
    "- ⏱️ 实现一个基于字符的简单计速——time 模块\n",
    "- 🪄 在Notebook IDE 中直接渲染 Markdown——display(Markdown())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 安装环境\n",
    "\n",
    "```bash\n",
    "conda create -n gradio_llama3 python=3.11 -y\n",
    "# Now activate it\n",
    "conda activate gradio_llama3\n",
    "\n",
    "# Install jupyter\n",
    "conda install jupyter -y\n",
    "```\n",
    "\n",
    "Groq API Key：[API KEYS](https://console.groq.com/keys)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "198.49s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n",
      "Requirement already satisfied: groq in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (0.5.0)\n",
      "Requirement already satisfied: Markdown in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (3.6)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from groq) (4.3.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from groq) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from groq) (0.27.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from groq) (2.7.1)\n",
      "Requirement already satisfied: sniffio in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from groq) (1.3.1)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.7 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from groq) (4.11.0)\n",
      "Requirement already satisfied: idna>=2.8 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from anyio<5,>=3.5.0->groq) (3.4)\n",
      "Requirement already satisfied: certifi in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from httpx<1,>=0.23.0->groq) (2024.2.2)\n",
      "Requirement already satisfied: httpcore==1.* in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from httpx<1,>=0.23.0->groq) (1.0.5)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->groq) (0.14.0)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->groq) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.18.2 in /Users/minp/miniconda3/envs/gradio_translation/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->groq) (2.18.2)\n"
     ]
    }
   ],
   "source": [
    "! pip install groq Markdown "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "生成速度:282字符/秒"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "**💸**沉没成本（Sunk Cost）解析\n",
       "=====================\n",
       "\n",
       "### 什么是沉没成本？\n",
       "\n",
       "沉没成本（Sunk Cost）是指已经发生的、不可回收的支出或投资。这些成本已经发生，不管未来如何决定，都不能改变或撤销。\n",
       "\n",
       "### 为什么沉没成本重要？\n",
       "\n",
       "沉没成本很重要，因为它们可能会影响我们的决策。我们常常会因为已经投入的成本而继续投资或维持某个项目，即使这个项目不再有价值。这是因为我们不想“浪费”已经投入的成本。\n",
       "\n",
       "### 例子\n",
       "\n",
       "* 你买了一张电影票，但是在电影开始前你不想去了。你仍然可以选择不去看电影，但你已经花了钱，这笔钱不能退款。\n",
       "* 你投资了一家公司，但这家公司现在正在亏损。你可以选择继续投资或撤销投资，但你已经投入的钱不能回收。\n",
       "\n",
       "### 沉没成本的陷阱\n",
       "\n",
       "沉没成本可能会导致以下几个问题：\n",
       "\n",
       "* **继续投资不良项目**：因为已经投入的成本，我们可能会继续投资一个不良项目，希望能够收回成本。\n",
       "* **错失其他机会**：因为沉没成本，我们可能会错失其他更好的投资机会。\n",
       "* **情感决策**：沉没成本可能会使我们基于情感做出决策，而不是基于理智。\n",
       "\n",
       "### 如何避免沉没成本的陷阱？\n",
       "\n",
       "* **理智决策**：在决策时，忽略已经投入的成本，基于当前的情况和未来的预期做出决策。\n",
       "* **重新评估**：重新评估当前的投资或项目，看看它们是否仍然有价值。\n",
       "* **割舍亏损**：如果一个项目不再有价值，及时割舍亏损，不要继续投入。"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import os\n",
    "from groq import Groq\n",
    "from IPython.display import display, Markdown\n",
    "import time\n",
    "\n",
    "groq_api_key = os.environ[\"GROQ_API_KEY\"]\n",
    "\n",
    "client = Groq(api_key=groq_api_key)\n",
    "start_time = time. time() # 开始计时\n",
    "chat_completion = client.chat.completions.create(\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"system\",\n",
    "            \"content\":\"\"\"\n",
    "            - Think step by step.\n",
    "            - Be precise, no preamble, get to the point.\n",
    "            - Always response in Simplified Chinese, unless user is in Eng Lish.\n",
    "            \"\"\"\n",
    "        },{\n",
    "            \"role\": \"user\",\n",
    "            \"content\":\"你是一位专业的经济学家，请用小白能理解的表达来详细解析沉没成本，输出为 Markdown 格式，大标题前方请附上 1 个合适的 Emoji\",\n",
    "        }\n",
    "        ],\n",
    "    model=\"llama3-70b-8192\",\n",
    "    temperature=0.8, max_tokens=4096,top_p=0.9,\n",
    ")\n",
    "\n",
    "end_time = time.time() # 结束计时\n",
    "elapsed_time = end_time - start_time # 计算生成时间\n",
    "generated_chars = len(chat_completion.choices[0].message.content) # 获取生成的字符数\n",
    "speed = generated_chars / elapsed_time # 计算生成速度\n",
    "\n",
    "display(Markdown(f\"生成速度:{speed:.0f}字符/秒\"))\n",
    "display(Markdown(chat_completion.choices[0].message.content))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 Azure TTS 将生成的文本朗读\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gradio_translation",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
